package de.hs_heilbronn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class WebService{
	
//gives back all books that are listed in the library database
public String allbooks(String a) {
	Database db = new Database();
	ResultSet rs = db.connectQuery("SELECT * FROM `buch` WHERE `studentid` is NULL;");
		   
	String books = "";
	
	try {
		int i = 0;
		while (rs.next()) {
			if(i !=0 ){
				books = books + (  ","+rs.getString(3) + " \n" +  rs.getString(4)  );
			}
			else{
				books = books + (  rs.getString(3)  + " \n"  + rs.getString(4) );
			}
			i++;
			
		}

	} catch (SQLException e) {
		e.printStackTrace();
	}
	return books;

}


//gives back all books that are reserved by myself
public String myBooks(int id) {
	//int ids = id.parseInt(id);
	Database db = new Database();
	ResultSet rs = db.connectQuery("SELECT * FROM buch WHERE studentid = " + id+";");
		   
	String books = "";
	
	try {
		int i =0;
		while (rs.next()) {
			if(i !=0){
				books = books + (  ","+rs.getString(3) + " \n " + rs.getString(4)  );
			}
			else{
				books = books + (  rs.getString(3) + " \n "  +  rs.getString(4)  );
			}
			i++;
			
		}

	} catch (SQLException e) {
		e.printStackTrace();
	}
	
	return books;

}


//gives a reserved book free so someone else can reserve it.
public String returnbooks(String name) {
	Database db = new Database();
	try{
	db.connectUpdate("UPDATE `buch` SET `studentid` = NULL WHERE `titel` = " + "'"+ name + "'" +";");
	
	return "Das Buch wurde zurückgegeben";
	}catch (Exception e){
	return "Es ist ein Fehler aufgetreten. Versuchen Sie es später erneut";	
	}
	
	
	

}

//returns a list of books that are not available at the time
public String lendBooks(String name) {
	Database db = new Database();
	try{
	db.connectUpdate("UPDATE `buch` SET `studentid`=  80312  WHERE `titel` = " + '"'+ name + '"' +";");
	
	return "Das Buch wurde ausgeliehen ";
	}catch (Exception e){
	return "Es ist ein Fehler aufgetreten. Versuchen Sie es später erneut";	
	}

}


}



